package com.example.polarity;

import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.widget.ProgressBar;


public class Player {
	
	public Bitmap PlayerImage;
	
	public float x = 0;
	public float y = 0;
	
	public float playerMagVX = 0;
	public float playerMagVY = 0;
	public Matrix matrix;
	public int MaxBoost = 100;
	public int BoostProgress = 100;
	public Player(Bitmap playerImage, String playerClass)
	{
		this.PlayerImage = playerImage;
		matrix = new Matrix();
	}
	
	public void DrawPlayer(Canvas canvas)
	{
		this.x = 50;
		
		if(this.PlayerImage != null)
		{
			this.y = (canvas.getHeight() / 2) - PlayerImage.getHeight();
			canvas.drawBitmap(this.PlayerImage, this.x, this.y, null);
		}
	}
	
	public void Move(Canvas canvas, GameView parent, Joystick joystick, boolean UseBoost, ProgressBar BoostBar)
	{
		float dx = 0;
		float dy = 0;
		float fx = 0;
		float fy = 0;
		int mass = 5;
		
		
		for(int i = 0; i < parent.MagnetList.size(); i++)
		{
			
			if(parent.MagnetList.get(i).magnetType == 0)
			{
				Magnet curMagnet = parent.MagnetList.get(i);
				
				dx = this.x - curMagnet.getMagnetX();
				dy = this.y - curMagnet.getMagnetY();
				
				float dist = (float)(dx * dx + dy * dy);
				
				fx -= mass * (dx / dist);
				fy -= mass *  (dy / dist);
			}
		}
		
		for(int i = 0; i < parent.MagnetList.size(); i++)
		{
			
			if(parent.MagnetList.get(i).magnetType == 1)
			{
				Magnet curMagnet = parent.MagnetList.get(i);
				
				dx = this.x - curMagnet.getMagnetX();
				dy = this.y - curMagnet.getMagnetY();
				
				float dist = (float)(dx * dx + dy * dy);
				
				fx += mass * (dx / dist);
				fy += mass *  (dy / dist);
			}
		}
		
		
		playerMagVX += fx;
		playerMagVY += fy;
		
		
		if(UseBoost)
		{
			
			playerMagVX += (float) Math.cos(Math.toRadians(joystick.angle)) * 0.09; 
			playerMagVY += (float) Math.sin(Math.toRadians(joystick.angle)) * 0.09;
			
			BoostProgress -= 1;
			
			BoostBar.setProgress(BoostProgress);
			
		}
		

		this.x += playerMagVX;
		this.y += playerMagVY;
		
		matrix.setTranslate(this.x, this.y);
		matrix.postRotate(joystick.angle, this.x + (this.PlayerImage.getWidth() /2), this.y + (this.PlayerImage.getHeight() / 2));
		
		canvas.drawBitmap(this.PlayerImage, matrix, null);
	}
	
	public void reset()
	{
		playerMagVX = 0;
		playerMagVY = 0;	
	}
}
